import { ref ,reactive,computed} from 'vue'
import { type } from 'windicss/utils'
import{toast} from '~/composables/util.js'

//列表，分页，搜索，删除，修改状态
export function useInitTable(opt={}) {

    // const searchForm = reactive({
    //     keyword: "",
    // })

    // const resetSearchForm = () => {
    //     searchForm.keyword = ""
    //     getData()
    // }
    let searchForm = null
    let resetSearchForm = null
    if(opt.searchForm){
        searchForm = reactive({...opt.searchForm})
        resetSearchForm = ()=>{
            for( const key in opt.searchForm){
                //初始对象对应的值
                searchForm[key] = opt.searchForm[key]
            }
            getData()
        }
    }
    // 绑定表格数据源
    const tableData = ref([])
    //加载动画
    const loading = ref(false)
    //分页
    const currentPage = ref(1) //当前分页的页码
    const total = ref(0) //总条数
    const limit = ref(10) //每页显示多少条

    //获取数据方法,p页码
    function getData(p = null) {
        if (typeof p == 'number') {
            currentPage.value = p
        }
        // console.log(p);
        loading.value = true
        //没写分页默认第一页
        opt.getList(currentPage.value, searchForm)
            .then(res => {
                if(opt.onGetListSuccess && typeof opt.onGetListSuccess == "function"){
                    opt.onGetListSuccess(res)
                }else{
                        tableData.value = res.list
                        total.value = res.totalCount
                }
                

            })
            .finally(() => {
                loading.value = false
            })
    }

    getData()

    //删除
const handleDelete = (id)=>{
    loading.value = true
    opt.delete(id)
    .then(res=>{
        toast("删除成功")
        getData()
    })
    .finally(()=>{
        loading.value = false
    })
}


//修改状态
const handleStatusChange = (status,row)=>{
    row.statusLoading = true
    opt.updateStatus(row.id,status)
    .then(res=>{
        toast("修改状态成功")
        //修改状态
        row.status = status
    })
    .finally(()=>{
        row.statusLoading = false
    })
}


//多选选中ID
const multiSelectionIds = ref([])
//定义清空选中的节点
const multipleTableRef = ref(null)
const handleSelectionChange = (e)=>{
    multiSelectionIds.value = e.map(o=>o.id);
}
// 调用删除方法
const handleMultiDelete = ()=>{
    loading.value = true;
    // console.log(multiSelectionIds.value);
    opt.delete(multiSelectionIds.value)
    .then(res=>{
        toast("删除成功")
        //清空选中
        if(multipleTableRef.value){
            multipleTableRef.value.clearSelection()
        }
        getData()
    })
    .finally(()=>{
        loading.value = false
    })
}

//批量修改状态的方法
const handleMultiStatusChange = (status)=>{
    loading.value = true;
    // console.log(multiSelectionIds.value);
    opt.updateStatus(multiSelectionIds.value,status)
    .then(res=>{
        toast("修改状态成功")
        //清空选中
        if(multipleTableRef.value){
            multipleTableRef.value.clearSelection()
        }
        getData()
    })
    .finally(()=>{
        loading.value = false
    })
}
    return{
        searchForm,
        resetSearchForm,
        tableData,
        loading,
        currentPage,
        total,
        limit,
        getData,
        handleDelete,
        handleStatusChange,
        multipleTableRef,
        handleSelectionChange,
        handleMultiDelete,
        handleMultiStatusChange,
        multiSelectionIds,
    }

}

//新增，修改
export function useInitForm(opt={}) {
    //动态表单属性绑定
    const formDrawerRef = ref(null)
    const formRef = ref(null)
    const defaultForm = opt.form
    const form = reactive({})
    const rules = opt.rules || {}
    // 默认新增
    const editId = ref(0)
    //计算属性
    const drawerTitle = computed(() => editId.value ? "修改" : "新增")
    //提交表达修改
    const handleSubmit = () => {
        formRef.value.validate((valid) => {
            if (!valid) return
            formDrawerRef.value.showLoading()

            let body = {}
            if(opt.beforeSumbit && typeof opt.beforeSumbit =="function"){
                body=opt.beforeSumbit({...form})
            }else{
                body = form
            }
            const fun = editId.value ? opt.update(editId.value, body) : opt.create(body)


            fun.then(res => {
                toast(drawerTitle.value + "成功")
                //判断是新增就刷新第一页，否则修改刷新当前页
                opt.getData(editId.value ? false : 1)
                formDrawerRef.value.close()
            })
                .finally(() => {
                    formDrawerRef.value.hideLoading()
                })
        })
    }
    //重置表单
    function resetForm(row = false) {
        if (formRef.value) {
            formRef.value.clearValidate()
        }
        for( const key in defaultForm){
            //初始对象对应的值
            form[key] = row[key]
        }

    }

    //新增
    const handleCreate = () => {
        editId.value = 0
        resetForm(defaultForm)
        formDrawerRef.value.open()
    }

    //编辑
    const handleEdit = (row) => {
        editId.value = row.id
        resetForm(row)
        formDrawerRef.value.open()
    }

    return{
        formDrawerRef,
        formRef,
        form,
        rules,
        editId,
        drawerTitle,
        handleSubmit,
        resetForm,
        handleCreate,
        handleEdit,
    }
}

